'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 走马灯信息获取
 * @author sundbcn@gmail.com (sun debing)
 */

var _ = require('lodash');
var marqueeModel = require('../models/marqueeModel.js');
var moment = require('moment');

/**
 * 走马灯信息获取
 * @param  {[type]} equip_type [description]
 * @return {[type]}            [description]
 */

exports.getMarqueeMsg = function (marquee_all_list) {

    var is_times_list = _getIstimeItem(marquee_all_list);

    // 定时任务刷选
    if (is_times_list.times_list_length > 0) {
        return is_times_list.times_list[0];
    } else {

        //优先级别刷选 priority
        return _getPriorityItem(marquee_all_list);
    }
};

// 取定时任务数据(优先级别)
var _getPriorityItem = function (marquee_all_list) {
    var now_task_list = [];
    _.some(_.sortBy(marquee_all_list, 'sort_id'), function (value) {
        if (value.is_time <= 0) {
            now_task_list.push(value);
        }
    });

    var item_num = now_task_list.length - 1;
    return now_task_list[item_num];
};

// 取定时任务数据(按定时时间)
var _getIstimeItem = function (marquee_all_list) {
    var times_list = [];
    var now_time = moment().unix();
    var is_vaild = false;

    // 刷选出定时任务列表
    _.some(marquee_all_list, function (send_item) {
        if (send_item.is_time == 1) {
            if (send_item.create_time <= now_time) {
                times_list.push(send_item);
                return true;
            }
        }
    });
    return {
        times_list: times_list,
        times_list_length: times_list.length
    };
};

// 数据库中移除信息
var _remove = function (list) {
    var remove_id = list._id;
    marqueeModel.del(remove_id, function (err, res) {});
};
